<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 广告管理模块
 *
 * @author www.dingwei.cn
 */
class AdmAdsModel extends MY_Model{
	
	public $tbAds = 'admin_ads';

    /**
     * 获取列表数据
     * @return 	array
     * @author heyu
     */
    public function adminPcGetList($ci = array()){

        $ads_position = (int)get('ads_position');
        $ads_name = trim(get('ads_name'));
        $status = (int)get('status');
        $startTimeFrom = get('start_time_from');
        $startTimeTo = get('start_time_to');
        $endTimeFrom = get('end_time_from');
        $endTimeTo = get('end_time_to');
        $send_date_from = get('send_date_from');
        $send_date_to = get('send_date_to');
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if ($ads_position){
            $this->db->where(array('a.ads_position' => $ads_position));
        }
        if ($ads_name){
            $this->db->like(array('a.ads_name' => $ads_name));
        }
        if ($status){
            $this->db->like(array('a.status' => $status));
        }
        if (strlen($startTimeFrom) && strlen($startTimeTo)){
            $this->db->where(array('a.starttime >= ' => strtotime($startTimeFrom), 'a.starttime <= ' => strtotime($startTimeTo)));
        }
        if (strlen($endTimeFrom) && strlen($endTimeTo)){
            $this->db->where(array('a.endtime >= ' => strtotime($endTimeFrom), 'a.endtime <= ' => strtotime($endTimeTo)));
        }
        if (strlen($send_date_from) && strlen($send_date_to)){
            $this->db->where(array('a.addtime >= ' => strtotime($send_date_from), 'a.addtime <= ' => strtotime($send_date_to)));
        }

        if($iSortCol_0 == 3){
            $this->db->order_by('a.starttime '.$sSortDir_0); // 开始时间
        }elseif ($iSortCol_0 == 4){
            $this->db->order_by('a.endtime '.$sSortDir_0); // 结束时间
        }elseif ($iSortCol_0 == 6){
            $this->db->order_by('a.addtime '.$sSortDir_0); // 添加时间
        }else{
            $this->db->order_by('a.addtime desc');
        }
        get_instance()->loadModel('MerchantUserModel', 'mMerchant', 'merchant');
        get_instance()->loadModel('ConsumerCategoryModel', 'mCategory', 'consumer');
        get_instance()->loadModel('ConsumerProductModel', 'mProduct', 'consumer');

        $this->db->select('a.*,s.shop_name as merchant_name,c.cat_name,p.name as product_name')
            ->join($this->mMerchant->tbUser.' mer', 'a.merchant_id=mer.id', 'left')
            ->join($this->mMerchant->tbShop.' s', 's.merchant_id=mer.id', 'left')
            ->join($this->mCategory->tbProductCategory.' c', 'a.param=c.id and a.type = 4', 'left')
            ->join($this->mProduct->tbProduct.' p', 'a.param=p.id and a.type = 3', 'left')
            ->where('a.is_delete is null');
        if (!empty($ci)){
            $this->db
                ->where_in('s.county', $ci);
        }
        $data = $this->pagination($this->tbAds.' a');

        return $data;
 	}

    /**
     * 获取广告详情
     * @param $id
     * @return array
     * @author heyu
     */
 	public function getInfo($id){
        if (!$id){
            return array();
        }
        $this->db->where(array('id' => $id));

        $tA = $this->getOne($this->tbAds);
        if($tA){
            $tA['pic'] = base_url($tA['pic']);
        }
        return $tA;
    }

    /**
     * 根据广告位查询
     * @param $position
     * @param $category_id      int     分类id
     * @param $notId            int     排除的id
     * @return array
     * @author heyu
     */
    public function getInfoByPosition($position, $category_id = 0, $notId = 0){
        if (!$position){
            return array();
        }
        if($category_id){
            $this->db->where(array('category_id' => $category_id));
        }
        if ($notId){
            $this->db->where('id != '.$notId);
        }
        $this->db->select('id,ads_name,pic,ads_position,merchant_id,starttime,endtime')
            ->where(array('ads_position' => $position))
            ->where('is_delete is null')
            ->order_by('addtime desc');

        $tA = $this->getAll($this->tbAds);

        foreach ($tA as $k=>&$v){
        	$v['pic'] = getImage($v['pic']);
        }
        return $tA;
    }

    /**
     * 保存
     * @param $data
     * @param int $id
     * @return bool|int
     * @author heyu
     */
    public function save($data, $id = 0){
        if($id){
            $this->db->where('id', $id);
            return $this->update($this->tbAds, $data);
        }

        return $this->insert($this->tbAds, $data);
    }

    /**
     * 删除广告
     * @param $id
     * @return bool
     * @author heyu
     */
    public function deleteAds($id){
        if (!$id){
            return false;
        }
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbAds);
    }

    ///////////////////////////////////////////////////////////////
    //+------------------------------------------------------
    //| app接口 获取广告
    //+------------------------------------------------------
    
    /**
     * 获取广告
     * @param array     $position	    位置值
     * @param integer   $merchantId     商家id
     * @param integer   $status 	    状态
     * @param boolean   $source 	    是否获取源数据
     * @param integer   $categoryId 	分类id
     * @return array
     * @author zyl
     */
    public function getAdsList($position = array(), $merchantId = 0,$status = 0,$source = false, $categoryId = 0){
  
    	if ($position){
    		//位置
    		$this->db->where_in('ads_position',$position);
    	}
    	
    	if ($merchantId){
    		$this->db->where('merchant_id',$merchantId);
    	}
        $this->db->select('ads_position,pic,url,merchant_id,type,param');
        $this->db->where('is_delete is null');
    	$tA = $this->getAll($this->tbAds);

    	if ($source){
    		//获取源数据
    		return $tA;
    	}elseif( ! $source){
    		$tData = array(
    				'lunbo'=>array(

    				),
                    "home_middle_left"=>array(
                        "ads_position"=>"0",
                        "imgs"=> getDefaultAds('ads'),
                        "url"=>"",
                        "type"=>"",
                        "param"=>"",
                        "merchant_id"=>"",
                    ),
                    "home_middle_right_top"=>array(
                        "ads_position"=>"0",
                        "imgs"=>getDefaultAds('lunbo'),
                        "url"=>"",
                        "type"=>"",
                        "param"=>"",
                        "merchant_id"=>"",
                    ),
                    "home_middle_right_down"=>array(
                        "ads_position"=>"0",
                        "imgs"=>getDefaultAds('lunbo'),
                        "url"=>"",
                        "type"=>"",
                        "param"=>"",
                        "merchant_id"=>"",
                    )
            );

	    	foreach ($tA as $k=>$v){
	    		
	    		if( ! isset($v['ads_position'])){
	    			return array();
	    		}
	    		if ($v['type'] == 1){
                    $param = $v['url'];
                }else if($v['type'] == 2){
                    $param = $v['merchant_id'];
                }else{
                    $param = isset($v['param']) ? $v['param'] : '';
                }

	    		if ($v['ads_position'] == 2){
	    			//轮播图
	    			$tData['lunbo'][] = array(
                            'imgs'=> isset($v['pic']) ? getImage($v['pic'],'og') : getDefaultAds('banner'),
	    					'url'=> $v['merchant_id'] > 0 ? '' : $v['url'],
	    					'merchant_id'=>$v['merchant_id'],
	    					'type'=>$v['type'],
	    					'param'=>$param,
	    					'ads_position'=>$v['ads_position'],
	    			);
	    		}
	    		else{
	    			//广告
	    			$opstion = isset($this->ADS_POSTION[$v['ads_position']]) ? $this->ADS_POSTION[$v['ads_position']]: '';
	    			$tData[$opstion] = array(
                        'ads_position'=>$v['ads_position'],
                        'imgs'=>getImage($v['pic'],'og'),
                        'url'=> $v['merchant_id'] > 0 ? '' : $v['url'],
                        'merchant_id'=>$v['merchant_id'],
                        'type'=>$v['type'],
                        'param'=>$param,
	    			);
	    		}
	    		
	    	}
	    	
	    	if ( ! $tData['lunbo']){
	    		//如果轮播为空 默认图
	    		$tData['lunbo'][] = array(
	    				'imgs'=>getDefaultAds('banner'),
	    				'url'=> '',
	    				'merchant_id'=>'0',
	    				'type'=>'0',
	    				'param'=>'0',
	    				'ads_position'=>'0',
	    		);
	    	}
	    	
	    	return $tData;
    	}
    	
    }
    
    
}